package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.domain.SysSale;
import com.ruoyi.system.service.ISysSaleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/sale")
public class SysSaleController extends BaseController
{
    private String prefix = "system/sale";

    @Autowired
    private ISysSaleService saleService;

    @RequiresPermissions("system:sale:view")
    @GetMapping()
    public String sale()
    {
        return prefix + "/sale";
    }

    @RequiresPermissions("system:sale:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSale sale)
    {
        startPage();
        List<SysSale> list = saleService.selectSaleList(sale);
        return getDataTable(list);
    }

    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:sale:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSale sale)
    {
        List<SysSale> list = saleService.selectSaleList(sale);
        ExcelUtil<SysSale> util = new ExcelUtil<>(SysSale.class);
        return util.exportExcel(list, "售后列表");
    }

    /**
     * 新增设备
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备
     */
    @RequiresPermissions("system:sale:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysSale sale)
    {
        sale.setCreateBy(getLoginName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(saleService.insertSale(sale));

    }

    /**
     * 修改设备
     */
    @RequiresPermissions("system:sale:edit")
    @GetMapping("/edit/{saleId}")
    public String edit(@PathVariable("saleId") Long saleId, ModelMap mmap)
    {
        mmap.put("sale", saleService.selectSaleById(saleId));
        return prefix + "/edit";
    }

    /**
     * 修改保存设备
     */
    @RequiresPermissions("system:sale:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysSale sale)
    {
        sale.setUpdateBy(getLoginName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(saleService.updateSale(sale));
    }

    @RequiresPermissions("system:sale:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(saleService.deleteSaleByIds(ids));
    }

    @RequiresPermissions("system:sale:detail")
    @GetMapping("/detail/{saleId}")
    public String detail(@PathVariable("saleId") Long saleId, ModelMap mmap)
    {
//        Syssale sale = saleService.selectsaleById(saleId);
        mmap.put("sale", saleService.selectSaleById(saleId));
        return prefix + "/detail";
    }
}
